from Model.models import WordVector

class Distince(object):
    def getVector(self,word):
        return [word.w1, word.w2, word.w3, word.w4, word.w5, word.w6, word.w7, word.w8, word.w9,
        word.w10, word.w11, word.w12, word.w13, word.w14, word.w15, word.w16, word.w17, word.w18, word.w19,
        word.w20, word.w21, word.w22, word.w23, word.w24, word.w25, word.w26, word.w27, word.w28, word.w29,
        word.w30, word.w31, word.w32, word.w33, word.w34, word.w35, word.w36, word.w37, word.w38, word.w39,
        word.w40, word.w41, word.w42, word.w43, word.w44, word.w45, word.w46, word.w47, word.w48, word.w49,
        word.w50]

    # 计算两个单词间的余弦距离
    @classmethod
    def CalculateWordDistance(self,W1,W2):
        # methods 1 欧式距离
        wl1 = self.getVector(self,W1)
        wl2 = self.getVector(self,W2)
        wlen = len(wl1)
        pow1 = 0
        pow2 = 0
        cor=0
        for ix in range(wlen):
            pow1 += wl1[ix]**2
            pow2 += wl2[ix]**2
            cor += wl1[ix]*wl2[ix]
        cos = cor / ((pow1*pow2)**0.5)
        return 1-cos